package org.bonorsoft.modules.control.controller;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Places;
import org.bonorsoft.modules.control.service.IPlacesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.modules.control.service.ISxPhotoService;
import org.bonorsoft.modules.control.vo.PlacesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * @Description: 四闲场所
 * @Author: WilliamYe
 * @Date: 2021-04-16
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/control/places")
public class PlacesController extends BonorController<Places, IPlacesService> {
    private String prefix = "control/places";

    @Autowired
    private IPlacesService placesService;

    @Autowired
    private ISxPhotoService sxPhotoService;


    /**
     * 首页
     */
    @RequiresPermissions("control:places:view")
    @GetMapping()
    public String index() {
        return prefix + "/places";
    }

    /**
     * 分页列表查询
     *
     * @param places
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "四闲场所-分页列表查询")
    @RequiresPermissions("control:places:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(Places places,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Places> queryWrapper = QueryGenerator.initQueryWrapper(places, req.getParameterMap());
        Page<Places> page = new Page<Places>(pageNo, pageSize);
        IPage<Places> pageList = placesService.page(page, queryWrapper);
        return Result.ok(pageList);
    }


    /**
     * 四闲场所详细信息页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap map) {
        map.put("places", placesService.getById(id));
        List<PlacesVo> places = placesService.listing(new QueryWrapper<Places>().eq("ywlsh", id));
        if (places.size() > 0) map.put("photo",sxPhotoService.getPhoto(id));
        return prefix + "/detail";
    }

    /**
     * 四闲场所二维码详细信息页面
     */
    @GetMapping("/qrcodeDetail/{id}")
    public String qrcodeDetail(@PathVariable("id") String id, ModelMap map) {
        map.put("places", placesService.getById(id));
        List<PlacesVo> places = placesService.listing(new QueryWrapper<Places>().eq("ywlsh", id));
        if (places.size() > 0) map.put("photo",sxPhotoService.getPhoto(id));
        return prefix + "/qrcodeDetail";
    }

    /**
     * 四闲场所二维码表格样式详细信息页面
     */
    @GetMapping("/qrcodeTable/{id}")
    public String qrcodeTable(@PathVariable("id") String id, ModelMap map) {
        map.put("places", placesService.getById(id));
        List<PlacesVo> places = placesService.listing(new QueryWrapper<Places>().eq("ywlsh", id));
        if (places.size() > 0) map.put("photo",sxPhotoService.getPhoto(id));
        return prefix + "/qrcodeTable";
    }

    /**
     *四闲场所二维码生成
     * @param id
     * @param map
     * @return
     */
    @GetMapping("/qrcode/{id}")
    public String qrcode(@PathVariable("id") String id, ModelMap map) {
        map.put("places", placesService.getById(id));
        return prefix + "/qrcode";
    }

    /**
     * 添加页面
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 添加
     *
     * @param places
     * @return
     */
    @AutoLog(value = "四闲场所-添加")
    @RequiresPermissions("control:places:add")
    @PostMapping(value = "/add")
    @ResponseBody
    public Result<?> add(@Validated Places places,String photo) {
        String qqid = UUID.randomUUID().toString().replaceAll("-", "");
        places.setYwlsh(qqid);
        placesService.save(places);
        if (oConvertUtils.isNotEmpty(photo)){
            sxPhotoService.addPhoto(qqid,photo);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("places", placesService.getById(id));
        map.put("photo",sxPhotoService.getPhotoes(id));
        return prefix + "/edit";
    }

    /**
     * 编辑
     *
     * @param places
     * @return
     */
    @AutoLog(value = "四闲场所-编辑")
    @RequiresPermissions("control:places:edit")
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result<?> edit(@Validated Places places, String photo) {
        String id = places.getYwlsh();
        placesService.updateById(places);
        if (oConvertUtils.isNotEmpty(photo)){
            sxPhotoService.updatePhoto(id,photo);
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "四闲场所-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        placesService.removeById(id);
        sxPhotoService.delete(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "四闲场所-批量删除")
    @RequiresPermissions("control:places:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    @ResponseBody
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        this.placesService.removeByIds(list);
        sxPhotoService.removeByIds(list);
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "四闲场所-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        Places places = placesService.getById(id);
        return Result.ok(places);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param places
     */
    @AutoLog(value = "四闲场所-导出excel")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Places places) {
        return super.exportXls(request, places, Places.class, "四闲场所");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @AutoLog(value = "四闲场所-通过excel导入数据")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Places.class);
    }

    /**
     * 模板下载
     *
     * @return
     */
    @AutoLog(value = "四闲场所-模板下载", operateType = 6)
    @RequestMapping(value = "/importTemplate")
    public ModelAndView importTemplate() {
        return super.importTemplateXls(Places.class, "四闲场所");
    }


}
